﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Data;
using DSPL.Common.CommonForms;
using System.Windows.Input;
using DSPL.Contracts;
using DSPL.Contracts.Interfaces;
using Microsoft.Practices.Unity;
using DSPL.Contracts.Common;

namespace DSPL.Common.CommonControls.Controls
{
    
    public class TxtDate : DatePicker
    {

        private DateTime _previousDate;

        #region Dependency Properties
       

        #endregion

        
        public DateTime PreviousDate
        {
            get
            {
                return _previousDate;
            }
            set
            {
                _previousDate = value;
            }

        }

        public IMasterReadService ReadFianancialData
        {
            get;
            set;
        }
                     
              

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            if (this.Text == string.Empty)
            {
                this.SelectedDate = DateTime.Today;
            }
           
        }


        protected override void OnSelectedDateChanged(SelectionChangedEventArgs e)
        {

            base.OnSelectedDateChanged(e);

            if (this.SelectedDate == null)
            {
                this.SelectedDate = DateTime.Today;
            }


            try
            {               
               
                DataTable dtTransactionPeriod = new DataTable();
                ResolveDependencies();
                if (ReadFianancialData != null)

                    dtTransactionPeriod = ReadFianancialData.GetConfigData();
                if (this.SelectedDate < Convert.ToDateTime(dtTransactionPeriod.Rows[0]["ConFtxn"]) || this.SelectedDate > Convert.ToDateTime(dtTransactionPeriod.Rows[0]["ConTtxn"]))
                {
                    MessageBox.Show("Invalid Date. Transaction period is " + Convert.ToDateTime(dtTransactionPeriod.Rows[0]["ConFtxn"]).ToString("dd/MM/yyyy") + " - " + Convert.ToDateTime(dtTransactionPeriod.Rows[0]["ConTtxn"]).ToString("dd/MM/yyyy"), "Transaction Date", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                if (this.SelectedDate < Convert.ToDateTime(dtTransactionPeriod.Rows[0]["ConFyear"]) || this.SelectedDate > Convert.ToDateTime(dtTransactionPeriod.Rows[0]["ConTyear"]))
                {
                    MessageBox.Show("Invalid Date. Financial period is " + Convert.ToDateTime(dtTransactionPeriod.Rows[0]["ConFyear"]).ToString("dd/MM/yyyy") + " - " + Convert.ToDateTime(dtTransactionPeriod.Rows[0]["ConTyear"]).ToString("dd/MM/yyyy"), "Transaction Date", MessageBoxButton.OK, MessageBoxImage.Information);
                    this.SelectedDate = PreviousDate;
                    return;
                }
            }

            catch (Exception)
            {
            }

        }

        private void ResolveDependencies()
        {
             IUnityContainer container = GlobalVariables.GlobalUnityContainer;
            if (container != null)
            {
                          
                if (ReadFianancialData == null)
                {
                    ReadFianancialData = container.Resolve<IMasterReadService>();
                }
            }
        }

       
    }
}
